package com.shaoshaohuo.app.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.BaseUserInfo;
import com.shaoshaohuo.app.entity.UserInfoEntity;
import com.shaoshaohuo.app.framework.AccountManager;
import com.shaoshaohuo.app.framework.SharedPreferencesHelper;
import com.shaoshaohuo.app.net.HttpRequest;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.utils.StringUtil;
import com.shaoshaohuo.app.view.TopbarView;

/**
 * 实名认证
 * Created by cao on 2016/3/21.
 */
public class AuthNameActivity extends BaseActivity implements View.OnClickListener {

    public static final String EXTRAS_USERNAME = "EXTRAS_USERNAME";

    private TopbarView mTopbarView;

    private EditText mNameEdit;
    private EditText mIdnumText;
    private Button mMyInfoButton;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auth_name);
        initView();
        setupView();
        requestData();
    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

        mNameEdit = (EditText) findViewById(R.id.edittext_name);
        mIdnumText = (EditText) findViewById(R.id.textview_idnum);
        mMyInfoButton = (Button) findViewById(R.id.button_my_info_ok);

    }

    /**
     * 访问远程
     */
    private void requestData() {
        RequestService.getInstance().getUserInfo(this, UserInfoEntity.class, new HttpRequest.RequestListener() {
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                if (resultData != null && resultData.isOk()) {
                    UserInfoEntity userEntity = (UserInfoEntity) resultData;
                    UserInfoEntity.UserInfo user = userEntity.getData();
                    BaseUserInfo userInfo = user.getBaseinfo();
                    if (userInfo != null) {
                        String verifyStatus = user.getVerifystatus();
                        if ("1".equals(verifyStatus) || "3".equals(verifyStatus)) {
                            mNameEdit.setText(userInfo.getRealname());
                            mIdnumText.setText(StringUtil.formatIdNum(userInfo.getIdnum()));
                            mNameEdit.setEnabled(false);
                            mIdnumText.setEnabled(false);
                            mMyInfoButton.setVisibility(View.GONE);
                        } else {
                            mMyInfoButton.setOnClickListener(AuthNameActivity.this);
                            String username =  getIntent().getStringExtra(EXTRAS_USERNAME);
                            if (username != null) {
                                mNameEdit.setText(username);
                            }
                        }
                    }

                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {

            }
        });
    }


    private void setupView() {

        mTopbarView.setCenterText("实名认证");
        mTopbarView.setLeftView(true, true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_my_info_ok:
                requestInfo();
                break;
        }
    }


    private void requestInfo() {


        String readName = mNameEdit.getText().toString().trim();
        String mIdnum = mIdnumText.getText().toString().trim();
        if (TextUtils.isEmpty(readName)) {
            showToast("请输入您的姓名");
            return;
        }
        if (TextUtils.isEmpty(mIdnum)) {
            showToast("请输入您的身份证号码");
            return;
        }

        startLoadingDialog();
        RequestService.getInstance().auditRealName(this, readName, mIdnum,
                BaseEntity.class, new HttpRequest.RequestListener() {

                    @Override
                    public void onSuccess(int requestCode, BaseEntity resultData) {
                        dismissLoadingDialog();
                        if (resultData.isOk()) {

                            // 提交以后不可以进行重复提交
                            SharedPreferencesHelper.setBoolean(AuthNameActivity.this,
                                    SharedPreferencesHelper.Field.IS_COME_BACK, true);
                            showToast("已提交，请等待审核");
                            AccountManager.updateUserInfo(AuthNameActivity.this);
                            finish();
                        } else {
                            showToast(resultData.getMsg());
                        }
                    }

                    @Override
                    public void onFailed(int requestCode, Exception error, String msg) {
                        dismissLoadingDialog();
                        showToast(msg);
                    }
                });
    }
}
